package com.online.controller;

import com.alibaba.fastjson.JSONObject;
import com.online.entity.CorrectJsonObject;
import com.online.entity.ErrorJsonObject;
import com.online.entity.User;
import com.online.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 测试
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/com/online", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String test(@RequestParam(required = false) Integer id) {
        User user = userService.showByid(id);
        if (user != null) {
            CorrectJsonObject correctJsonObject = new CorrectJsonObject("查询成功");
            correctJsonObject.setData(user);
            return JSONObject.toJSONString(correctJsonObject);
        } else {
            ErrorJsonObject errorJsonObject = new ErrorJsonObject(103, "查询失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
    }

    /**
     * 用户登录
     *
     * @param userNumber
     * @param password
     * @param request
     * @return
     */

    @RequestMapping(value = "/userLogin", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    public String userLogin(@RequestParam(required = false) String userNumber, @RequestParam(required = false) String password, HttpServletRequest request) {

        userNumber = request.getParameter("userNumber");
        password = request.getParameter("password");
        System.out.println(userNumber);
        System.out.println(password);

        Subject subject = SecurityUtils.getSubject();
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(userNumber, password);
            subject.login(token);

            //判断用户登录状态
            int status = userService.showStatusByUserNumber(userNumber);
            if (status != 0) {
                request.setAttribute("error", "账号已被冻结，请联系管理员.");
                return "user/userLogin";
            }

            return "user/index";
        } catch (Exception e) {
            request.setAttribute("error","账号或密码错误,请重新输入.");
            return "user/userLogin";
        }
    }

    /**
     * 用户登出操作
     * @return
     */
    @RequestMapping(value = "/userLogout",method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    public String userLogout() {

        Subject subject = SecurityUtils.getSubject();
        //用户登出
        subject.logout();

        return "user/userLogin";
    }
}
